<?php
class My_EventContentController extends MM_Controller_My_EventDetailAction
{	
	/**
	 * var
	 */
	private $ecid = null;
	
	/**
	 * @var MM_Service_Admin_EventContent
	 */
	private $serviceEC = null;
	
	/**
	 * 初始化
	 */
	public function init()
	{
		parent::init();
		$this->serviceEC = new MM_Service_Admin_EventContent();
	}
	
	/**
	 * index
	 */
	public function indexAction()
	{
		$eid = $this->httpdata["eid"];
		$this->pageview->assign("eventid",$eid);
		$this->helper->setPageView($this->pageview, WEB_TITLE, "eventcontent", null);
	}
	
	/**
	 * page
	 */
	public function pageAction()
	{
		if ( !empty($this->httpdata['act']) && isset($this->httpdata['act']) ) {
			
			$where = null;
			$order = "num asc";
			if(isset($this->httpdata['eid'])) {
				$where = sprintf("eid = '%s'",$this->httpdata['eid']);
			}
			$result = $this->serviceEC->fetchAll(null,null,$where,$order);
			if ($result->getCode()==$result::CODE_FAILURE) echo $this->encodeResult($result);
			$data = $result->getData();
			$dt = new Mediad2_Utility_Datatable($data);
			
			$dt->init($this->httpdata['act'], array("title"));
			$dt->create();
			$dt->response();
		}
	}
	
	/**
	 * get event content
	 */
	public function listAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$where = "eid = $this->eid";
			$order = "ecid asc";
			$this->result = $this->serviceEC->fetchAll(null,null,$where,$order,true);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * create event content
	 */
	public function createAction()
	{
		if ( ($this->uid<1) || ($this->eid<1)
				|| !isset($this->httpdata['ectype']) 
				|| !isset($this->httpdata['title']) || empty($this->httpdata['title']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/title...');
		} else {
			//封装model
			$model = new MM_Model_EventContent();
			$model->setEctype($this->httpdata['ectype']);
			$model->setEid($this->eid);
			$model->setEsid(empty($this->httpdata['esid'])?0:$this->httpdata['esid']);
			$model->setTitle($this->httpdata['title']);
			$model->setNum(empty($this->httpdata['num'])?0:$this->httpdata['num']);
			$model->setStatus($model::STATUS_YES);
			$model->setAttachment($this->httpdata['attachment']);
			$model->setCanuse($this->httpdata['canuse']);
			$model->setFid($this->httpdata['fid']);
			$model->setFilepath($this->httpdata['filepath']);
			$model->setUid($this->uid);
			$model->setAddtime(time());
		
			if (isset($this->httpdata['content'])) $model->setContent($this->httpdata['content']);
			if (isset($this->httpdata['url'])) $model->setUrl($this->httpdata['url']);
		
			$this->result = $this->serviceEC->insert($model);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * update event content
	 */
	public function updateAction()
	{
		if ( ($this->uid<1)
				|| !isset($this->httpdata['ecid']) || empty($this->httpdata['ecid']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/ecid');
		} else {
			$ecid = $this->httpdata['ecid'];
			$this->result = $this->serviceEC->updateContent($ecid, $this->httpdata);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * delete event content
	 */
	public function delAction()
	{
		if ( ($this->uid<1)
				|| !isset($this->httpdata['ecid']) || empty($this->httpdata['ecid']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/ecid');
		} else {
			$ecid = $this->httpdata['ecid'];
			$this->result = $this->serviceEC->delete($ecid);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get event content type from dict
	 */
	public function getectypeAction()
	{
		if ( ($this->uid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid');
		} else {
			$result = $this->serviceEC->getEventContentType();
			$contents = $result->getData();
			$output =null;
			foreach ($contents as $key => $value) {
				if($key=="name"||$key=="num"){
					foreach ($value as $k => $v) {
						$output[$k][$key] = $v;
					}
				}
			}
			$this->result = $this->returnResult($result->getCode(),null,$output);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get event content 
	 */
	public function getAction()
	{
		if ( ($this->uid<1)
				|| !isset($this->httpdata['ecid']) || empty($this->httpdata['ecid']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/ecid');
		} else {
			$this->result = $this->serviceEC->find($this->httpdata['ecid'],true);
		}
		echo $this->encodeResult($this->result);
	}
}